//
//  ServiceActiveTableViewCell.m
//  GAMCAfterSales
//
//  Created by BigAmazing on 2018/7/18.
//  Copyright © 2018年 glendale. All rights reserved.
//

#import "ServiceActiveTableViewCell.h"
#import "popContentViewController.h"
#import "DDPopoverBackgroundView.h"
#import "MainMenuDataModel.h"
@interface ServiceActiveTableViewCell()<ContentViewControllerDelegate>{
    //下拉菜单属性
    UIPopoverController *popCon;
}

@property(nonatomic,strong)popContentViewController *contentCon;


/**
 选择按钮
 */
@property (weak, nonatomic) IBOutlet UIButton *chooseBtn;
/**
 服务活动代码
 */

@property (weak, nonatomic) IBOutlet UILabel *serviceCodeLbl;
/**
 服务活动名称
 */

@property (weak, nonatomic) IBOutlet UILabel *serviceNameLbl;
/**
 服务活动类型
 */

@property (weak, nonatomic) IBOutlet UILabel *activeTypeLbl;
/**
截止时间
*/

@property (weak, nonatomic) IBOutlet UILabel *endDateLbl;
/**
不参加原因按钮
*/

@property (weak, nonatomic) IBOutlet UIButton *reasonBtn;
/**
其他原因输入框
*/

@property (weak, nonatomic) IBOutlet UITextField *otherReasonTxt;

@property(nonatomic,strong)NSString * reasonCodeStr;


@property(nonatomic,strong)NSMutableDictionary * updateReasonDic;
@end
@implementation ServiceActiveTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    [self.otherReasonTxt addTarget:self action:@selector(changeReson:) forControlEvents:UIControlEventEditingChanged];
}
-(void)changeReson:(UITextField*)textField{
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    NSArray * tmpArr = [NSArray arrayWithArray:modelIns.activeReasonArr];
    for (NSDictionary * dic in tmpArr) {
        if ([dic[@"ACTIVITY_CODE"] isEqualToString:self.serviceDic[@"ACTIVITY_CODE"]]){
            self.updateReasonDic[@"OTHER_REASON"]=textField.text;
            [modelIns.activeReasonArr removeObject:dic];
            [modelIns.activeReasonArr addObject:self.updateReasonDic];
        }
    }
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)setServiceDic:(NSDictionary *)serviceDic{
//
    _serviceDic = serviceDic;
    self.chooseBtn.selected = YES;
    self.serviceCodeLbl.text = _serviceDic[@"ACTIVITY_CODE"];
    self.serviceNameLbl.text = _serviceDic[@"ACTIVITY_NAME"];
    self.activeTypeLbl.text =  _serviceDic[@"ACTIVITY_SOURCE_NAME"] ;
    self.endDateLbl.text = [_serviceDic[@"END_DATE"] substringToIndex:9];
    self.otherReasonTxt.enabled = NO;
    
    
}
//选择按钮
- (IBAction)ChooseBtnClick:(UIButton *)sender {
    sender.selected = !sender.selected;

      MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    if (sender.selected) {
//        [self.updateReasonDic removeAllObjects];
        self.reasonBtn.enabled = NO;
        self.otherReasonTxt.enabled = NO;
        [self.reasonBtn setTitle:@"" forState:UIControlStateNormal];
        self.otherReasonTxt.text=@"";
        
        NSMutableArray * arr = [[NSMutableArray alloc]init];
        arr = modelIns.activeReasonArr;
        NSArray * tmpArr = [NSArray arrayWithArray:arr];

        for (NSDictionary * dic in tmpArr) {
            if ([dic[@"ACTIVITY_CODE"] isEqualToString:self.serviceDic[@"ACTIVITY_CODE"]]) {
                [arr removeObject:dic];
            }
        }
        
        modelIns.activeReasonArr = arr;
    }else{
        self.reasonBtn.enabled = YES;

        [self.updateReasonDic setValue:self.serviceDic[@"ACTIVITY_CODE"] forKey:@"ACTIVITY_CODE"];//活动代码
        [self.updateReasonDic setValue:self.serviceDic[@"ACTIVITY_NAME"] forKey:@"ACTIVITY_NAME"];
        [self.updateReasonDic setValue:@"A" forKey:@"UPDATE_STATUS"];
        [self.updateReasonDic setValue:self.vin forKey:@"VIN"];
        [self.updateReasonDic setValue:@"" forKey:@"MAIN_REASON_CODE"];
        [self.updateReasonDic setValue:@"" forKey:@"MAIN_REASON"];
        
        [self.updateReasonDic setValue:@"" forKey:@"OTHER_REASON"];
        [self.updateReasonDic setValue:@"" forKey:@"ASC_NAME"];
        [modelIns.activeReasonArr addObject:self.updateReasonDic];

    }
    
        if (self.delegate && [self.delegate respondsToSelector:@selector(chooseSelectedBtn:ActivityCode:)]) {
            [self.delegate chooseSelectedBtn:sender.selected ActivityCode:self.serviceDic[@"ACTIVITY_CODE"]];
        }
    NSLog(@"reason===%@",modelIns.activeReasonArr);

}

//原因按钮
- (IBAction)ReasonClick:(UIButton *)sender {
    //创建下拉菜单
    _contentCon.list = nil;
    [self createThePopViewControl:sender];
    NSArray *source = self.reasonNameArr;
    _contentCon.list = source;
    [_contentCon.myTableView reloadData];
    
}

//创建下拉菜单方法
-(void)createThePopViewControl:(UIButton *)sender
{
    if (!_contentCon)
    {
        _contentCon=[[popContentViewController alloc] initWithNibName:@"popContentViewController" bundle:nil];
        _contentCon.delegate = self;
    }
    
    _contentCon.btnSender=sender;
    
    popCon = [[UIPopoverController alloc] initWithContentViewController:_contentCon];
    [DDPopoverBackgroundView setTintColor:[UIColor colorWithRed:234 green:248 blue:222 alpha:0.9]];
    [DDPopoverBackgroundView setArrowBase:30.0];//箭头
    [DDPopoverBackgroundView setArrowHeight:15.0];
    [DDPopoverBackgroundView setContentInset:1.0];
    
    [popCon setPopoverBackgroundViewClass:[DDPopoverBackgroundView class]];
    [popCon setPopoverContentSize:CGSizeMake(sender.frame.size.width, 200)];
    
    [popCon presentPopoverFromRect:CGRectMake(20, 10, 70, 20) inView:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
    
    
}

#pragma mark ---- 弹出的回调方法
-(void)selectDelegate:(NSInteger)row btn:(UIButton *)btn
{
    NSString *key = nil;
     MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    if (btn == self.reasonBtn) {
        key = self.reasonNameArr[row];
        self.reasonCodeStr = self.reasonCodeArr[row];
        NSMutableArray * arr = [[NSMutableArray alloc]init];
        
        arr =modelIns.activeReasonArr;
        NSArray * tmpArr = [NSArray arrayWithArray:arr];

        for (NSDictionary * dic in tmpArr) {
            if ([dic[@"ACTIVITY_CODE"] isEqualToString:self.serviceDic[@"ACTIVITY_CODE"]]){
                [self.updateReasonDic setValue:self.reasonCodeStr forKey:@"MAIN_REASON_CODE"];
                 [self.updateReasonDic setValue:key forKey:@"MAIN_REASON"];
                [modelIns.activeReasonArr removeObject:dic];
                [modelIns.activeReasonArr addObject:self.updateReasonDic];
            }
        }
    }
    if ([self.reasonCodeStr isEqualToString:@"90501005"]) {
        self.otherReasonTxt.enabled = YES;
    }
 
    [btn setTitle:key forState:UIControlStateNormal];
   
    //清空数据，退出控件
    [popCon dismissPopoverAnimated:YES];
    popCon = nil;
}

-(NSMutableDictionary *)updateReasonDic{
    if (_updateReasonDic == nil) {
        _updateReasonDic = [[NSMutableDictionary alloc]init];
    }
    return _updateReasonDic;
}
@end
